package com.molyfun.parents.view.zhibo;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.zhibo.im.ImModel;

import java.util.LinkedList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 *
 */
public class ZhiboImAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private List<ImModel> mList = new LinkedList<>();

    private static final int MAX_CHACHE_MSG_COUNT = 200;

    private static final int TYPE_ZHIBO_NOTICE = 1001;
    private static final int TYPE_ZHIBO_CONTENT = 1002;

    private String mTips;


    public ZhiboImAdapter(Context context, String tips) {
        this.mContext = context;
        ImModel imModel = new ImModel();

        ImModel.UserCountBody userCountBody = new ImModel.UserCountBody(tips);
        imModel.setEventType("live");
        imModel.setOpertype("system");
        imModel.setData(userCountBody);
        ImModel.IMUserModel imUserModel = new ImModel.IMUserModel();
        imUserModel.setUserName("系统提示");
        imModel.setUser(imUserModel);
        mList.add(imModel);
        notifyDataSetChanged();
    }

    /**
     * 用户seek到某个位置后，需要调用这个方法
     *
     * @param imModels
     */
    public void refreshData(List<ImModel> imModels) {
        mList.clear();
        mList.addAll(imModels);
        notifyDataSetChanged();
    }

    public void addMsg(ImModel imModel) {
        if (mList.size() > MAX_CHACHE_MSG_COUNT) {
            mList = mList.subList(5, mList.size() - 1);
        }
        mList.add(imModel);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        ImModel imModel = mList.get(position);
        if(!TextUtils.isEmpty(imModel.getOpertype())){
            if(imModel.getOpertype().equals("system")){
                return TYPE_ZHIBO_NOTICE;
            }else{
                return TYPE_ZHIBO_CONTENT;
            }
        }else{
            return TYPE_ZHIBO_CONTENT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case TYPE_ZHIBO_CONTENT:
                return new ZhiboImViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.layout_zhibo_im_list, parent, false));
            case TYPE_ZHIBO_NOTICE:
                default:
                return new ZhiboHeaderViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.layout_zhibo_system_notice, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ImModel imModel = mList.get(position);
        switch (getItemViewType(position)){
            case TYPE_ZHIBO_CONTENT:
                ZhiboImViewHolder zhiboImViewHolder = (ZhiboImViewHolder) holder;
                zhiboImViewHolder.mUserName.setTextColor(ContextCompat.getColor(mContext, R.color.color_title_text));
                zhiboImViewHolder.mUserMsg.setTextColor(ContextCompat.getColor(mContext, R.color.color_title_text));
                zhiboImViewHolder.mUserMsg.setBackgroundResource(R.drawable.bg_translucent);
                zhiboImViewHolder.mUserMsg.setTextSize(14);
                zhiboImViewHolder.mAskIcon.setVisibility(View.INVISIBLE);

                zhiboImViewHolder.mUserName.setText(imModel.getUser().getUserName() + ": ");
                zhiboImViewHolder.mUserMsg.setPadding(0, mContext.getResources().getDimensionPixelSize(R.dimen.common_normal_top_padding)
                        , mContext.getResources().getDimensionPixelSize(R.dimen.common_normal_padding), mContext.getResources().getDimensionPixelSize(R.dimen.common_normal_bottom_padding));

                if (imModel.getOpertype().equals("enter")) {
                    zhiboImViewHolder.mUserMsg.setText("进入了房间");
                } else if (imModel.getOpertype().equals("leave")) {
                    zhiboImViewHolder.mUserMsg.setText("离开了房间");
                } else if (imModel.getOpertype().equals("groupmsg")) {
                    ImModel.StringMsgBody stringMsgBody = (ImModel.StringMsgBody) imModel.getData();
                    zhiboImViewHolder.mUserName.setText(stringMsgBody.getUsername() + ": ");
                    zhiboImViewHolder.mUserMsg.setText(stringMsgBody.getContent());
                    if (!TextUtils.isEmpty(stringMsgBody.getType()) && stringMsgBody.getType().equals("ques")) {
                        zhiboImViewHolder.mUserMsg.setBackgroundResource(R.drawable.bg_rounded_base);
                        zhiboImViewHolder.mAskIcon.setVisibility(View.VISIBLE);
                        zhiboImViewHolder.mUserMsg.setTextColor(ContextCompat.getColor(mContext, R.color.color_white));
                        zhiboImViewHolder.mUserMsg.setPadding(mContext.getResources().getDimensionPixelSize(R.dimen.common_normal_padding), mContext.getResources().getDimensionPixelSize(R.dimen.common_normal_top_padding)
                                , mContext.getResources().getDimensionPixelSize(R.dimen.common_normal_padding), mContext.getResources().getDimensionPixelSize(R.dimen.common_normal_bottom_padding));
                    }
                }
                break;
            case TYPE_ZHIBO_NOTICE:
                ZhiboHeaderViewHolder zhiboHeaderViewHolder = (ZhiboHeaderViewHolder)holder;
                zhiboHeaderViewHolder.mSysNotice.setText(imModel.getUser().getUserName() + ": " + ((ImModel.UserCountBody) imModel.getData()).getUsercount());

                break;
        }



    }

    public class ZhiboImViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.im_ask_icon)
        ImageView mAskIcon;
        @InjectView(R.id.im_user_name)
        TextView mUserName;
        @InjectView(R.id.im_user_msg)
        TextView mUserMsg;

        public ZhiboImViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public class ZhiboHeaderViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.system_content)
        TextView mSysNotice;

        public ZhiboHeaderViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 0;
        }
        return mList.size();
    }

}
